package com.lz.spring.cloud.study.control;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.lz.spring.cloud.study.service.DemoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
public class DemoControl {

	private static final Logger LOG = LoggerFactory.getLogger(DemoControl.class);

	@Autowired
    private DemoService demoService;

	@GetMapping("test00")
	public void test00(){
		Entry entry = null;
		try {
			entry = SphU.entry("HelloWorld");
			// 资源中的逻辑.
			LOG.info("hello world");
		} catch (BlockException e1) {
			System.out.println("blocked!");
		} finally {
			if (entry != null) {
				entry.exit();
			}
		}
	}
	@GetMapping("test01")
	public void test01(){
		while (true) {
			Entry entry = null;
			try {
				entry = SphU.entry("HelloWorld");
				// 资源中的逻辑.
				LOG.info("hello world");
				TimeUnit.MILLISECONDS.sleep(40L);
			} catch (BlockException e1) {
				System.out.println("blocked!");
			} catch (InterruptedException e2) {
				e2.printStackTrace();
			} finally {
				if (entry != null) {
					entry.exit();
				}
			}
		}
	}

	@GetMapping("test02")
	public String test02(){
        while (true) {
            LOG.info(demoService.hello(System.currentTimeMillis()));
            try {
                TimeUnit.MILLISECONDS.sleep(40L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
	}
}
